<template>
  <el-dialog
    :title="titles"
    :visible="showDialog"
    width="400px"
    @close="btnCancel"
  >
    <el-form ref="formDate" :model="formData" label-width="80px" :rules="rules">
      <el-form-item label="所属学科" prop="subjectID" v-show="!subject">
        <el-select
          v-model="formData.subjectID"
          placeholder="请选择"
          style="width: 87%"
        >
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.subjectName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName" style="width: 90%">
        <el-input
          v-model="formData.directoryName"
          placeholder="请填写目录名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <!-- 底部栏 插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="end" style="width:280px">
        <el-col style="justify-content: flex-end;">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确认</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { add, update } from "@/api/hmmm/directorys";
import { subjectsList } from "@/api/hmmm/subjects";
export default {
  props: {
    showDialog: {
      // 弹层是否显示
      type: Boolean,
      default: false,
    },
    subject: {
      default: false,
    },
  },
  data() {
    return {
      formData: {
        directoryName: "", // 目录名称
        subjectName: "", // 所属学科
        id: "", // 当前数据的id
        subjectID: "", //  所属学科的id
      },
      rules: {
        subjectID: [
          { required: true, trigger: "change", message: "请选择所属学科" },
        ],
        directoryName: [
          { required: true, trigger: "blur", message: "请填写目录名称" },
        ],
      },
      titles: "新增目录",
      list: {},
    };
  },
  async created() {
    const { data } = await subjectsList();
    this.list = data.items;
  },
  methods: {
    // 修改
    async updateDirectorys(date, id) {
      // 获取沈学科数据
      const { data } = await subjectsList();
      this.list = data.items;
      if (date.id) {
        this.titles = "修改目录";
        //   console.log(111)
        this.formData = date;
        //   console.log(date);
        //   console.log(222);
        this.formData.subjectName = date.subjectName;
        this.formData.directoryName = date.directoryName;
      } else {
        if (id) {
          this.formData.subjectID = id;
        }
        this.titles = "新增目录";
      }
    },
    //  点击确定
    async btnOk() {
      try {
        // 手动校验
        await this.$refs.formDate.validate();
        if (this.formData.id) {
          // 修改页面
          //  console.log(11);
          await update(this.formData);
          console.log(this.formData);
        } else {
          // 新增页面
          await add(this.formData);
        }
        this.$emit("update:showDialog", false);
        this.$message.success("新增成功");
        this.$emit("Getlist");
      } catch (error) {
        console.log(error);
      }
    },
    //  点击取消
    btnCancel() {
      // 重置数据和校验
      (this.formData = {}), this.$refs.formDate.resetFields();
      // 关闭弹层
      this.$emit("update:showDialog", false);
    },
  },
};
</script>

<style scoped lang='less'>

</style>
